package com.example.sinoyd.frameapplication.ProjectCodes.Adapter

import android.content.Context
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.RelativeLayout
import android.widget.TextView
import com.example.sinoyd.frameapplication.ProjectCodes.Dataclass.GetAllAQI
import com.example.sinoyd.frameapplication.R
import com.sinoyd.Code.Until.SharedPreferencesFactory

/**
 * 作者： scj
 * 创建时间： 2018/1/15
 * 版权： 二维列表中，中间的girdview适配器
 * 描述： com.sinoyd.Code.Adapter
 */

class GisAdapter(var list: ArrayList<GetAllAQI.AQIInfoBean>, var context: Context) : BaseAdapter() {
    var paramsonlyone = RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)

    override fun getView(p0: Int, convertView: View?, p2: ViewGroup?): View {
        var view: View
        var holder: ViewHolder?
        if (convertView == null) {
            view = LayoutInflater.from(context).inflate(R.layout.item_gis_layout, null)
            holder = ViewHolder(view)
            view.tag = holder
        } else {
            view = convertView
            holder = view.tag as ViewHolder
        }


        holder.left!!.text = list[p0].pointName
        holder.middle!!.text = list[p0].aqi
        holder.right!!.text = list[p0].grade

        when (SharedPreferencesFactory.getdata(context, "OS")) {
            "air" -> {
                holder.middle!!.visibility = View.VISIBLE
            }
            "water" -> {
                holder.middle!!.visibility = View.GONE
            }
        }

        when (list[p0].grade) {
            "一级", "I类" -> {
                holder.right!!.setBackgroundColor(Color.parseColor("#0FCD05"))
            }
            "二级", "II类" -> {
                holder.right!!.setBackgroundColor(Color.parseColor("#ffff00"))
            }
            "三级", "III类" -> {
                holder.right!!.setBackgroundColor(Color.parseColor("#FE7E00"))
            }
            "四级", "IV类" -> {
                holder.right!!.setBackgroundColor(Color.parseColor("#FE1D00"))
            }
            "五级", "V类" -> {
                holder.right!!.setBackgroundColor(Color.parseColor("#7E0923"))
            }
            "劣V类" -> {
                holder.right!!.setBackgroundColor(Color.parseColor("#7E0923"))
            }
            else -> {
                holder.right!!.setBackgroundColor(Color.parseColor("#4a4e4a"))
            }
        }

        //判断第几行进行背景配置
        if (p0 % 2 == 0) {
            //奇数行
            view.setBackgroundColor(Color.argb(20, 0, 0, 0))
        } else {
            //欧数行
            view.setBackgroundColor(Color.argb(0, 0, 0, 0))
        }



        return view

    }

    override fun getItem(p0: Int): Any = list[p0]

    override fun getItemId(p0: Int): Long = 0L

    override fun getCount(): Int = list.size


    internal inner class ViewHolder(view: View) {
        var left: TextView? = null
        var middle: TextView? = null
        var right: TextView? = null

        init {
            left = view.findViewById(R.id.left)
            middle = view.findViewById(R.id.middle)
            right = view.findViewById(R.id.right)
        }
    }
}